package org.springframework.boot;

import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.EnvironmentPostProcessorApplicationListener;
import org.springframework.boot.env.RandomValuePropertySourceEnvironmentPostProcessor;
import org.springframework.boot.logging.DeferredLog;
import org.springframework.boot.logging.DeferredLogs;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.SpringFactoriesLoader;

import javax.swing.*;
import java.util.List;

/****
 * 后处理器增强 
 * @author Administrator
 *
 */
public class Step5 {
    public static void main(String[] args) {
        test2(args);

    }

    /****
     * 模拟读取配置文件的方式来实现增强
     * @param args
     */
    private static void test2(String[] args) {
    	  SpringApplication app = new SpringApplication();
          app.addListeners(new EnvironmentPostProcessorApplicationListener()); //得到环境后置处理器

          EventPublishingRunListener publisher = new EventPublishingRunListener(app, args);//得到事件发布器
          ApplicationEnvironment env = new ApplicationEnvironment();
          System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强前");
          for (PropertySource  ps : env.getPropertySources()) {
              System.out.println(ps);
          }
          publisher.environmentPrepared(new DefaultBootstrapContext(), env); //调用事件增强
          System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后");
          for (PropertySource  ps : env.getPropertySources()) {
              System.out.println(ps);
          }
    }
    
    /***
     * 使用后处理器来增强
     */
    private static void test1() {
        SpringApplication app = new SpringApplication();
        ApplicationEnvironment env = new ApplicationEnvironment();

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强前");
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        }
        //后处理器增强   ConfigDataEnvironmentPostProcessor   读取application配置文件
        ConfigDataEnvironmentPostProcessor postProcessor1 = new ConfigDataEnvironmentPostProcessor(new DeferredLogs(), new DefaultBootstrapContext());
        postProcessor1.postProcessEnvironment(env, app);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后");
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        }
        System.out.println(env.getProperty("server.port"));
        
        //后处理增强  RandomValuePropertySourceEnvironmentPostProcessor  得到随机的值
        RandomValuePropertySourceEnvironmentPostProcessor postProcessor2 = new RandomValuePropertySourceEnvironmentPostProcessor(new DeferredLog());
        postProcessor2.postProcessEnvironment(env, app);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>> 增强后");
        for (PropertySource<?> ps : env.getPropertySources()) {
            System.out.println(ps);
        }
      
        System.out.println(env.getProperty("random.int"));
        System.out.println(env.getProperty("random.int"));
        System.out.println(env.getProperty("random.int"));
        System.out.println(env.getProperty("random.uuid"));
        System.out.println(env.getProperty("random.uuid"));
        System.out.println(env.getProperty("random.uuid"));
    }
}
